#include "client4.h"
#include "ui_client4.h"
#include "client3.h"

client4::client4(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::client4)
{
    ui->setupUi(this);
    setWindowTitle(tr("小翼音乐播放器"));
    //音乐播放器操作
    player=new QMediaPlayer(this);
    playlist=new QMediaPlaylist(this);
    playlist->setPlaybackMode(QMediaPlaylist::Loop);
    player->setPlaylist(playlist);
    //音乐播放器槽函数
    connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64)));
    connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(onDurationChanged(qint64)));

    //链接信号和槽
    QObject::connect(player,&QMediaPlayer::durationChanged,this,[&](quint64 duration){
        //周期改变(歌曲时间显示出来)
        //   水平滑块          设置进度条范围  周期
        ui->horizontalScrollBar->setRange(0,duration);
        //文本显示歌曲周期，以字符串打印
        music_duration=QString("%1:%2").arg(duration/1000/60).arg(duration/1000%60);
        ui->label_5->setText(QString("%1").arg(music_duration));
        //显示歌曲切换      当前歌曲      播放列表     当前时长
        ui->listWidget->setCurrentRow(playlist->currentIndex());
    });
    //
    QObject::connect(player,&QMediaPlayer::positionChanged,this,[&](quint64 position){
        //进度改变(进度条的更新)   设置值    位置
        ui->horizontalScrollBar->setValue(position);
        //文本显示歌曲实时进度以字符串的形式打印
        music_duratio=QString("%1:%2").arg(position/1000/60).arg(position/1000%60);
        ui->label_12->setText(QString("%1").arg(music_duratio));
    });

}

client4::~client4()
{
    delete ui;

}
//添加音乐
void client4::on_pushButton_5_clicked()
{
    QStringList music_files = QFileDialog::getOpenFileNames(this,"选择音乐文件","D:/音乐");
    //去重
    for(int i = 0;i<ui->listWidget->count();i++)
    {
        for(int j = 0;j<music_files.count();j++)
        {
            if(ui->listWidget->item(i)->text()==music_files.at(j))
            {
                music_files.removeAt(j);
                break;
            }
        }
    }
    if(music_files.isEmpty())
        return ;

    for(int i = 0;i<music_files.count();i++)
    {
        QString file = music_files.at(i);
        //文件加到播放列表
        playlist->addMedia(QUrl::fromLocalFile(file));
    }
    ui->listWidget->addItems(music_files);
    ui->listWidget->setCurrentRow(0);

}
//删除音乐
void client4::on_pushButton_6_clicked()
{
    int row = ui->listWidget->currentRow();

    QListWidgetItem* item = ui->listWidget->takeItem(row);

    if(item)
    {
        delete item;
    }

    playlist->removeMedia(row);

}
//清空音乐

void client4::on_pushButton_7_clicked()
{
    ui->listWidget->clear();
    playlist->clear();
}

void client4::on_pushButton_8_clicked()
{
    int row=ui->listWidget->currentRow();
    playlist->setCurrentIndex(row);
    player->play();
    player->setVolume(ui->verticalSlider->value());
    ui->pushButton_10->setText(tr("暂停"));
    ui->pushButton_10->setEnabled(true);

}

void client4::on_pushButton_9_clicked()
{
    player->stop();

    ui->pushButton_10->setText(tr("暂停"));
    ui->pushButton_10->setEnabled(false);
}

void client4::on_pushButton_10_clicked()
{
    if(player->state()==QMediaPlayer::PlayingState)
    {
        player->pause();
        ui->pushButton_10->setText(tr("继续"));
    }
    else if(player->state()==QMediaPlayer::PausedState)
    {
        player->play();

        ui->pushButton_10->setText(tr("暂停"));
    }

}

void client4::on_pushButton_11_clicked()
{
    int row=ui->listWidget->currentRow();
    if(row==0)
        row=ui->listWidget->count();
    else
        row--;
    ui->listWidget->setCurrentRow(row);
    playlist->setCurrentIndex(row);
    player->play();
    player->setVolume(ui->verticalSlider->value());
    ui->pushButton_10->setText(tr("暂停"));
    ui->pushButton_10->setEnabled(true);
}

void client4::on_pushButton_12_clicked()
{
    int row=ui->listWidget->currentRow();
    if(row==ui->listWidget->count()-1)
        row=0;
    else
        row++;
    ui->listWidget->setCurrentRow(row);
    playlist->setCurrentIndex(row);
    player->play();
    player->setVolume(ui->verticalSlider->value());
    ui->pushButton_10->setText(tr("暂停"));
    ui->pushButton_10->setEnabled(false);
}

void client4::on_verticalSlider_actionTriggered(int action)
{
    player->setVolume(action);
    ui->label_6->setText(QString("音量：%1").arg(action));
}

void client4::on_horizontalScrollBar_actionTriggered(int action)
{
    player->setPosition(action);
}

void client4::on_commandLinkButton_clicked()
{
    client3 *p = new client3;
    p->show();
    hide();
}
